﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Threading;

namespace RpgEditor
{
    public partial class mainForm : Form
    {

        public SpriteEditor se;
        public Map map;
        public Event eve;
        public role r;
        public war w;
        public Item it;
        public skill sk;
        public monster m;
        public bool dragFrameFrag = false;
        public int dragFrameFragindex = -1;
        public bool dragFrag = false;
        public bool dragFragToFrame = false;

        public mainForm()
        {
            InitializeComponent();
            se = new SpriteEditor(this);
            map = new Map(this);
            eve = new Event(this);
            r = new role(this);
            w = new war(this);
            it = new Item(this);
            sk = new skill(this);
            m = new monster(this);

            Event.funPara[0] = textBox15;
            Event.funPara[1] = textBox16;
            Event.funPara[2] = textBox17;
            Event.funPara[3] = textBox18;
            Event.funPara[4] = textBox19;
            Event.funPara[5] = textBox20;
            Event.funPara[6] = textBox21;
            Event.funPara[7] = textBox22;
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "动画文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                se.setFn(openFileDialog1.FileName);
                se.readObject(se.getFn());
                se.display();
            }
            
        }

        private void listBox1_DoubleClick_1(object sender, EventArgs e)
        {
            // play sprite                                   
            int select = listBox1.SelectedIndex;
            if (select > -1)
            {
                //se.displaySprite(select);
                se.selectsprite = select;
                
                if (backgroundWorker1.IsBusy)
                {
                    //MessageBox.Show("当前正在播放其他动画，先停止它才能播放");
                    
                    se.displayspritestatus = false;
                    return;
                    //////Thread.Sleep(2000);
                    ////int c = 0;
                    ////while (backgroundWorker1.IsBusy && c < 10000)
                    ////{
                    ////    se.displayspritestatus = true;
                    ////    c++;
                    ////}

                }
                
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "动画文件(*.dat)|*.dat";
            if (se.getFn() == null)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    se.setFn( saveFileDialog1.FileName);
                    se.Save();
                }
            }
            else
            {
                se.Save();
            }
        }

        private void spritePage_Paint(object sender, PaintEventArgs e)
        {
           
        }

        private void button6_Click(object sender, EventArgs e)
        {
            se.display();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "动画文件(*.dat)|*.dat";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                se.setFn(saveFileDialog1.FileName);
                se.Save();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            // 增加图片
            openFileDialog1.Filter = "图片文件(*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                se.addImg(openFileDialog1.FileName);
                se.display();
            }
        }

        private void listBox2_Click(object sender, EventArgs e)
        {
            // 改变 listbox3

        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //se.displayFrag();
            //在右边显示该图片
            if (this.listBox2.SelectedIndex > -1)
            {
                se.displayBigImg();
                se.displayFragList(false);
                se.drawfrag();
            }
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox3.SelectedIndex != -1 && listBox2.SelectedIndex != -1)
            {
                textBox2.Text = (String)listBox3.Items[listBox3.SelectedIndex];
                se.drawfrag();
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (listBox3.SelectedIndex != -1 && listBox2.SelectedIndex != -1)
            {
                se.changeFrag(listBox2.SelectedIndex, listBox3.SelectedIndex, textBox2.Text);
            }
            else
            {
                MessageBox.Show("请先选中图片列表和左边列表！", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            //se.displayBigImg();
            se.displayFragList(true);
            se.drawfrag();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex != -1)
            {
                se.addNullFrag(listBox2.SelectedIndex);
            }
            //se.displayBigImg();
            se.displayFragList(true);
            se.drawfrag();
        }

        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex != -1)
            {
                textBox3.Lines = se.getFrameValue(listBox4.SelectedIndex);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            se.addNullFrame();
            se.displayFrameList(2);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex != -1)
            {
                if (!se.changeFrame(listBox4.SelectedIndex, textBox3.Lines))
                {
                    MessageBox.Show("修改值失败，请检查值是否合法，并重新打开文件！", "Error",
             MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
                else
                {
                    this.listBox4_Click(sender, e);
                }
            }
            else
            {
                MessageBox.Show("请先选中左边列表！", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            se.addSprite("new");
            se.displaySpriteList(2);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1 && se.sprites != null)
            {
                Sprite s = (Sprite)se.sprites[listBox1.SelectedIndex];
                textBox4.Text = s.name;
                textBox5.Text = s.getFrameList();
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                se.sprites.RemoveAt(listBox1.SelectedIndex);
                se.displaySpriteList(1);
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                se.changeSprite(listBox1.SelectedIndex, textBox4.Text, textBox5.Text);
                se.displaySpriteList(1);
            }
            else
            {
                MessageBox.Show("请先选中左边列表！", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void listBox3_Click(object sender, EventArgs e)
        {
            
        }

        private void listBox4_Click(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex != -1)
            {
                
                Graphics g = panel3.CreateGraphics();
                Rectangle rect1 = new Rectangle();
                rect1.X = 0;
                rect1.Y = 0;
                rect1.Height = panel3.Height;
                rect1.Width = panel3.Width;
                g.SetClip(rect1);
                g.Clear(BackColor);
                ArrayList frame = (ArrayList)se.tmpFrame[listBox4.SelectedIndex];
                se.displayFrame(g, frame,true);
                listBox4_SelectedIndexChanged(sender, e);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            se.displayspritestatus = false;
            se.init();
            se.displayFrameList(0);
            se.displayFragList(false);
            se.displaySpriteList(0);

            se.displayImgList();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "地图文件(*.me)|*.me";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                map.setFn(openFileDialog1.FileName);
                map.loadLocalResource(map.getFn());
                map.displayMap(-1,-1,0);
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            map.displayMap(-1, -1, 0);
        }

        private void panel10_Click(object sender, EventArgs e)
        {
            
        }
        public void SetCursor(Bitmap cursor, Point hotPoint)
        {
            int hotX = hotPoint.X;
            int hotY = hotPoint.Y;
            Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g = Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,
            cursor.Height);

            this.Cursor = new Cursor(myNewCursor.GetHicon());

            g.Dispose();
            myNewCursor.Dispose();
        }
        private void panel10_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int index = map.getindex(e.X, e.Y);
                if (index != -1)
                {
                    //ImageEx ti = map.img[index];

                    ////创建新图位图   
                    //Bitmap bitmap = new Bitmap(ti.w, ti.h);
                    //Graphics graphic = Graphics.FromImage(bitmap);
                    //graphic.DrawImage(ti.img, new Rectangle(-ti.x, -ti.y, ti.img.Width, ti.img.Height));

                    ////tmp[k] = Image.FromHbitmap(bitmap.GetHbitmap());

                    //SetCursor(bitmap, new Point(0, 0));
                    map.currindex = index;
                }
                
            }
            if (e.Button == MouseButtons.Right)
            {
                this.Cursor = Cursors.Default;
                map.currindex = -1;
            }
            map.displayImgList();
        }

        private void tabPage2_MouseDown(object sender, MouseEventArgs e)
        {
            
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (map.imglist == null)
            {
                MessageBox.Show("请先添加图片！", "Error",
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            saveFileDialog1.Filter = "地图文件(*.me)|*.me";
            if (map.getFn() == null)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        int id = Int32.Parse(Path.GetFileNameWithoutExtension(saveFileDialog1.FileName));
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show("文件必须以数字命名！", "Error",
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    map.setFn(saveFileDialog1.FileName);
                    map.save(map.getFn());
                }
            }
            else
            {
                map.save(map.getFn());
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            int tr = Int32.Parse(textBox8.Text);
            int tc = Int32.Parse(textBox9.Text);
            if(map.addimg(textBox7.Text,tr,tc))
           map.displayMap(-1,-1,0);
            else
           MessageBox.Show("添加图片失败，请检查各项参数！", "Error",
      MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void panel11_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (map.currindex != -1)
                {
                    if (map.setImg(e.X, e.Y,true))
                    {
                        Point p = map.getindex1(e.X, e.Y);
                         if (p.X != -1)
                         {
                             map.displayMap(p.X,p.Y,5);
                         }
                    }
                }
                else
                {
                    if (map.setblock(e.X, e.Y))
                    {
                        Point p = map.getindex1(e.X, e.Y);
                        if (p.X != -1)
                        {
                            map.displayMap(p.X, p.Y, 5);
                        }
                    }
                }
            }
        }

        private void button15_Click_1(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "地图文件(*.me)|*.me";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                map = new Map(this);
                map.setFn(saveFileDialog1.FileName);
                map.setRC(Int32.Parse(textBox11.Text), Int32.Parse(textBox10.Text));

                if (!map.hasjumpdata)
                {

                    Map.jumpdata.load(map.jumpfn);
                    map.hasjumpdata = true;
                }
                map.displayMap(-1,-1,0);
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "地图文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                eve.setFn(openFileDialog1.FileName);
                eve.loadEvent(eve.getFn());
                eve.display();
            }
        }

        private void listBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex != -1)
            {
                eve.displaymode(listBox5.SelectedIndex);
            }
        }

        private void listBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1)
            {
                eve.displaysub(listBox5.SelectedIndex,listBox6.SelectedIndex);

            }
        }

        private void button21_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "地图文件(*.dat)|*.dat";
            if (eve.getFn() == null)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    eve.setFn(saveFileDialog1.FileName);
                    eve.save(eve.getFn());
                }
            }
            else
            {
                eve.save(eve.getFn());
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            Event.subEvent = null;
            Event.subEvent = new Event[3][];
            eve.setFn(null);
            textBox13.Text = "";
            eve.display();
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex != -1)
            {
                eve.addSub(listBox5.SelectedIndex);
                eve.display();
            }
        }

        private void listBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1 && listBox7.SelectedIndex != -1)
            {
                eve.displaycondition(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox7.SelectedIndex);
            }
        }

        private void button23_Click(object sender, EventArgs e)
        {
            textBox14.Text = textBox14.Text.Replace("，", ",");
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1)
            {
                eve.addcondition(listBox5.SelectedIndex, listBox6.SelectedIndex,comboBox1.SelectedIndex,comboBox2.SelectedIndex,textBox14.Text);
                eve.displaysub(listBox5.SelectedIndex,listBox6.SelectedIndex);
            }
        }

        private void button24_Click(object sender, EventArgs e)
        {
            textBox14.Text = textBox14.Text.Replace("，", ",");
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1 && listBox7.SelectedIndex != -1)
            {
                eve.upcondition(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox7.SelectedIndex,comboBox1.SelectedIndex, comboBox2.SelectedIndex, textBox14.Text);
                eve.displaysub(listBox5.SelectedIndex, listBox6.SelectedIndex);
            }
        }

        private void button25_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex != -1 && listBox6.SelectedIndex != -1)
            {
                eve.delSub(listBox5.SelectedIndex,listBox6.SelectedIndex);
                eve.display();
            }
        }

        private void button26_Click(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1 && listBox7.SelectedIndex != -1)
            {
                eve.delcondition(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox7.SelectedIndex);
                eve.displaysub(listBox5.SelectedIndex, listBox6.SelectedIndex);
            }
        }

        private void button27_Click(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1)
            {
                eve.addfunction(listBox5.SelectedIndex, listBox6.SelectedIndex, comboBox3.SelectedIndex);
                eve.displaysub(listBox5.SelectedIndex, listBox6.SelectedIndex);
            }
        }

        private void button29_Click(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1 && listBox8.SelectedIndex != -1)
            {
                int index = listBox8.SelectedIndex;
                eve.upfunction(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox8.SelectedIndex, comboBox3.SelectedIndex);
                eve.displaysub(listBox5.SelectedIndex, listBox6.SelectedIndex);
                listBox8.SelectedIndex = index;
            }
        }

        private void button28_Click(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1 && listBox8.SelectedIndex != -1)
            {
                eve.delfunction(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox8.SelectedIndex);
                eve.displaysub(listBox5.SelectedIndex, listBox6.SelectedIndex);
            }
        }

        private void listBox8_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1 && listBox8.SelectedIndex != -1)
            {
                eve.displayfun(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox8.SelectedIndex);
            }
        }

        private void button32_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "角色文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                r.setFn(openFileDialog1.FileName);
                //listBox9.Items.Clear();
                r.getRoleInfo(r.getFn());
                r.display();
            }
        }

        private void listBox9_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox9.SelectedIndex != -1)
            {
                r.displayatt(listBox9.SelectedIndex);
            }
        }

        private void button30_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                r = new role(this);
                r.init();
                r.setFn(saveFileDialog1.FileName);
                r.display();
            }
        }

        private void button31_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "角色文件(*.dat)|*.dat";
            if (r.getFn() == null)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    r.setFn(saveFileDialog1.FileName);
                    r.save();
                }
            }
            else
            {
                r.save();
            }
        }

        private void button33_Click(object sender, EventArgs e)
        {
            if (listBox9.SelectedIndex != -1)
            {
                r.up(listBox9.SelectedIndex);
            }
        }

        private void button34_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "数据文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                w.setFn(openFileDialog1.FileName);
                //listBox9.Items.Clear();
                w.getwarInfo(w.getFn());
                w.display();
            }
        }

        private void button35_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                w = new war(this);
                w.init();
                w.setFn(saveFileDialog1.FileName);
                w.display();
            }
        }

        private void button36_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "数据文件(*.dat)|*.dat";
            if (w.getFn() == null)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    w.setFn(saveFileDialog1.FileName);
                    w.save();
                }
            }
            else
            {
                w.save();
            }
        }

        private void button37_Click(object sender, EventArgs e)
        {
            if (listBox10.SelectedIndex != -1)
            {
                w.up(listBox10.SelectedIndex);
            }
        }

        private void listBox10_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox10.SelectedIndex != -1)
            {
                w.displayatt(listBox10.SelectedIndex);
            }
        }

        private void button38_Click(object sender, EventArgs e)
        {
            w.add();
            w.display();
        }



        private void button39_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "数据文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                it.setFn(openFileDialog1.FileName);
                //listBox9.Items.Clear();
                it.getitemInfo(it.getFn());
                it.display();
            }
        }

        private void button40_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                it = new Item(this);
                it.init();
                it.setFn(saveFileDialog1.FileName);
                it.display();
            }
        }

        private void button41_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "数据文件(*.dat)|*.dat";
            if (it.getFn() == null)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    it.setFn(saveFileDialog1.FileName);
                    it.save();
                }
            }
            else
            {
                it.save();
            }
        }

        private void listBox11_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox11.SelectedIndex != -1)
            {
                it.displayatt(listBox11.SelectedIndex);
            }
        }

        private void button42_Click(object sender, EventArgs e)
        {
            it.add();
            it.display();
        }

        private void button43_Click(object sender, EventArgs e)
        {
            if (listBox11.SelectedIndex != -1)
            {
                it.up(listBox11.SelectedIndex);
            }
        }

        private void tabPage6_Click(object sender, EventArgs e)
        {

        }

        private void button44_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "数据文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                sk.setFn(openFileDialog1.FileName);
                //listBox9.Items.Clear();
                sk.getInfo(sk.getFn());
                sk.display();
            }
        }

        private void listBox12_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox12.SelectedIndex != -1)
            {
                sk.displayatt(listBox12.SelectedIndex);
            }
        }

        private void button47_Click(object sender, EventArgs e)
        {
            sk.add();
            sk.display();
        }

        private void button48_Click(object sender, EventArgs e)
        {
            if (listBox12.SelectedIndex != -1)
            {
                sk.up(listBox12.SelectedIndex);
            }
        }

        private void button45_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "数据文件(*.dat)|*.dat";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                sk = new skill(this);
                sk.setFn(saveFileDialog1.FileName);
                sk.init();
                sk.display();
            }
        }

        private void button46_Click(object sender, EventArgs e)
        {
            if (sk.getFn() == null)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    sk.setFn(saveFileDialog1.FileName);
                    sk.save();
                }
            }
            else
            {
                sk.save();
            }
        }

        private void button49_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "数据文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                m.setFn(openFileDialog1.FileName);
                //listBox9.Items.Clear();
                m.getInfo(m.getFn());
                m.display();
            }
        }

        private void button50_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                m = new monster(this);
                m.setFn(saveFileDialog1.FileName);
                m.init();
                m.display();
            }
        }

        private void button51_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "数据文件(*.dat)|*.dat";
            if (m.getFn() == null)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    m.setFn(saveFileDialog1.FileName);
                    m.save();
                }
            }
            else
            {
                m.save();
            }
        }

        private void listBox13_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox13.SelectedIndex != -1)
            {
                m.displayatt(listBox13.SelectedIndex);
            }
        }

        private void button52_Click(object sender, EventArgs e)
        {
            m.add();
            m.display();
        }

        private void button53_Click(object sender, EventArgs e)
        {
            if (listBox13.SelectedIndex != -1)
            {
                m.up(listBox13.SelectedIndex);
            }
        }

        private void listBox14_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox14.SelectedIndex != -1)
            {
                map.displayjump(listBox14.SelectedIndex);
            }
        }

        private void button54_Click(object sender, EventArgs e)
        {
            map.addjump();
            map.displayMap(-1,-1,0);
        }

        private void button55_Click(object sender, EventArgs e)
        {
            if (listBox14.SelectedIndex != -1)
            {
                map.upjump(listBox14.SelectedIndex);
            }
        }

        private void button56_Click(object sender, EventArgs e)
        {
            this.se.displayspritestatus = false;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            
            se.displaySprite();
        }



        private void button57_Click(object sender, EventArgs e)
        {
            if (se.imgrate < 10)
            {
                se.imgrate++;
                label89.Text = "倍率：*" + se.imgrate;
                se.displayBigImg();
                //se.displayFragList();
                se.drawfrag();
                this.listBox4_Click(sender, e);
            }
        }

        private void button58_Click(object sender, EventArgs e)
        {
            if (se.imgrate > 1)
            {
                se.imgrate--;
                label89.Text = "倍率：*" + se.imgrate;
                se.displayBigImg();
                //se.displayFragList();
                se.drawfrag();
                this.listBox4_Click(sender, e);
            }
        }



        int startx = 0;
        int starty = 0;
        sbyte transx = 0;
        sbyte transy = 0;

        private void panel3_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (dragFrameFrag)
                {
                    dragFrameFrag = false;
                    dragFrameFragindex = -1;
                }
                else
                {
                    int index = -1;
                    dragFrameFrag = false;
                    Rectangle rect;
                    if (e.Button == MouseButtons.Left)
                    {

                        // 开始拖动切片,根据xy判断切片
                        if (!se.displayspritestatus)
                        {
                            if (listBox4.SelectedIndex != -1)
                            {
                                if (se.frameFragRect != null)
                                {
                                    for (int i = 0; i < se.frameFragRect.Count; i++)
                                    {
                                        rect = (Rectangle)se.frameFragRect[i];
                                        if (rect.Contains(e.X, e.Y))
                                        {
                                            index = i;
                                        }
                                    }
                                }

                            }
                        }


                        if (index != -1)
                        {
                            dragFrameFrag = true;
                            dragFrameFragindex = index;
                            startx = e.X;
                            starty = e.Y;
                            ArrayList frame = (ArrayList)se.tmpFrame[listBox4.SelectedIndex];
                            ArrayList tl1 = (ArrayList)(frame[dragFrameFragindex]);

                            transx = (SByte)(tl1[3]);
                            //transx = transx * rate;
                            transy = (SByte)(tl1[4]);
                        }
                    }
                }
            }
            if (e.Button == MouseButtons.Right)
            {
                dragFrameFrag = false;
                if (dragFragToFrame)
                {
                    dragFragToFrame = false;
                    this.Cursor = Cursors.Default;
                    // 加个切片
                    String[] temp = new String[textBox3.Lines.Length + 1];
                    System.Array.Copy(textBox3.Lines, temp, textBox3.Lines.Length);
                    int tx = (e.X - panel3.Width/2)/se.imgrate;
                    int ty = (e.Y - panel3.Height/2)/se.imgrate;
                    temp[temp.Length - 1] = listBox2.SelectedIndex + "," + se.curfragindex + ","+ tx+","+ty;
                    textBox3.Lines = temp;
                    button11_Click(sender,e);
                    this.listBox4_Click(sender,e);
                }
            }
        }



        private void panel3_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragFrameFrag)
            {
                //textBox83.Text = "sx:" + startx + " sy:" + starty + " tx:" + transx + " ty:" + transy+" ex:"+e.X+" ey:"+e.Y;
                int tx = e.X - startx;
                int ty = e.Y - starty;

                ArrayList frame = (ArrayList)se.tmpFrame[listBox4.SelectedIndex];
                ArrayList tl1 = (ArrayList)(frame[dragFrameFragindex]);

                //SByte transx = (SByte)(tl1[3]);
                ////transx = transx * rate;
                //SByte transy = (SByte)(tl1[4]);          

                tl1[3] = (SByte)((SByte)(transx + (tx / se.imgrate)));
                tl1[4] = (SByte)((SByte)(transy + (ty / se.imgrate)));
                //startx = e.X;
                //starty = e.Y;
                
                this.listBox4_Click(sender, e);
            }
        }

        private void checkBox2_Click(object sender, EventArgs e)
        {
            this.listBox4_Click(sender, e);
        }

        private void panel24_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && !dragFragToFrame)
            {
                if (dragFrag)
                {
                    dragFrag = false;
                }
                else
                {
                    if (listBox2.SelectedIndex == -1)
                    {
                        MessageBox.Show("请先选中左边列表！", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    dragFrag = true;
                    startx = e.X;
                    starty = e.Y;
                    //增加一个切片
                    se.addNullFrag(listBox2.SelectedIndex);

                    se.displayFragList(true);
                    // 增加的在最后一行
                    listBox3.SelectedIndex = listBox3.Items.Count - 1;
                    se.drawfrag();

                }

            }
            if (e.Button == MouseButtons.Right && !dragFrag)
            {
                if (!dragFragToFrame && listBox2.SelectedIndex != -1)
                {
                    if (listBox4.SelectedIndex < 0)
                    {
                        MessageBox.Show("请先选中左边列表！", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    Rectangle rect = se.getIndexFrag(e.X, e.Y);
                    if (rect.Width != -1)
                    {
                        ImgFile tf = (ImgFile)se.spriteImg[listBox2.SelectedIndex];
                        Image ti = tf.img;
                        //创建新图位图   
                        Bitmap bitmap = new Bitmap(rect.Width, rect.Height);
                        Graphics graphic = Graphics.FromImage(bitmap);
                        //graphic.DrawImage(ti, rect);
                        graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                        graphic.DrawImage(ti, new Rectangle(0, 0, rect.Width, rect.Height), new Rectangle(rect.X / se.imgrate,rect.Y  / se.imgrate,rect.Width /se.imgrate,rect.Height/se.imgrate), GraphicsUnit.Pixel);
                        //tmp[k] = Image.FromHbitmap(bitmap.GetHbitmap());

                        SetCursor(bitmap, new Point(0, 0));
                        

                        dragFragToFrame = true;
                    }
                }
                else {
                    dragFragToFrame = false;
                    this.Cursor = Cursors.Default;
                }
            }

        }

        private void panel24_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (dragFrag)
                {
                    dragFrag = false;
        
                
                    listBox3.SelectedIndex = listBox3.Items.Count - 1;

                }
            }
            if (e.Button == MouseButtons.Right)
            {
            }
        }

        int ex = -1;
        int ey = -1;
        private void panel24_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragFrag  )
            {
                //textBox83.Text = "sx:" + startx + " sy:" + starty + " tx:" + transx + " ty:" + transy+" ex:"+e.X+" ey:"+e.Y;
                int tx = e.X;
                int ty = e.Y;
                if (ex == tx && ey == ty)
                {
                    return;
                }
                ex = tx;
                ey = ty;
                if (startx > e.X)
                {
                    tx = startx;
                    startx = e.X;
                }
                if (starty > e.Y)
                {
                    ty = starty;
                    starty = e.Y;
                }
                byte x = (byte)(startx / se.imgrate);
                byte y = (byte)(starty / se.imgrate);
                byte w = (byte)((tx - startx) / se.imgrate);
                byte h = (byte)((ty - starty) / se.imgrate);
                textBox2.Text = h + "," + w + "," + x + "," + y;
                se.changeFrag(listBox2.SelectedIndex, listBox3.Items.Count-1, textBox2.Text);

                se.displayFragList(true);
                se.drawfrag();
            }
        }

        private void button59_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex != -1)
            {
                if (MessageBox.Show("删除此项会影响相关的内容，是否删除？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    se.deleteimg(listBox2.SelectedIndex);
                    se.displayImgList();
                    se.displayFragList(false);
                    se.displayFrameList(1);
                }
            }
        }

        private void button60_Click(object sender, EventArgs e)
        {
            if (listBox3.SelectedIndex != -1 && listBox2.SelectedIndex != -1)
            {
                if (MessageBox.Show("删除此项会影响相关的内容，是否删除？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (se.tmpFragments != null)
                    {
                        ArrayList tl = (ArrayList)(se.tmpFragments[listBox2.SelectedIndex]);
                        int size = tl.Count / 4;
                        if (listBox3.SelectedIndex < size)
                        {
                            for (int i = (listBox3.SelectedIndex+1) * 4-1,count=0; i >= 0 && count < 4; i--,count++)
                            {
                                tl.RemoveAt(i);
                            }
                        }
                        
                        se.displayFragList(true);
                    }
                }
            }
        }

        private void button61_Click(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex != -1)
            {
                if (MessageBox.Show("删除此项会影响相关的内容，是否删除？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    // 删除相关frame
                    if (se.tmpFrame != null)
                    {
                        int index = listBox4.SelectedIndex;
                        se.tmpFrame.RemoveAt(index);
                        se.displayFrameList(1);
                        if(index < listBox4.Items.Count)
                        listBox4.SelectedIndex = index;
                        this.listBox4_Click(sender,e);
                    }
                }
            }
        }

        private void button62_Click(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex != -1 && listBox1.SelectedIndex != -1)
            {
                if (textBox5.Text.Equals(""))
                {
                    textBox5.Text = ""+listBox4.SelectedIndex;
                }
                else
                {
                    textBox5.Text = textBox5.Text + "," + listBox4.SelectedIndex;
                }
                button14_Click(sender, e);
            }
            else
            {
                MessageBox.Show("请先选中图片列表和左边列表！", "Error",
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void mainForm_Load(object sender, EventArgs e)
        {

        }

        private void button63_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex != -1 && listBox6.SelectedIndex != -1){
                eve.movesub(listBox5.SelectedIndex, listBox6.SelectedIndex, 0);
                eve.display();
            }
            else
            {
                MessageBox.Show("请先选中图片列表和左边列表！", "Error",
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button64_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex != -1 && listBox6.SelectedIndex != -1)
            {
                eve.movesub(listBox5.SelectedIndex, listBox6.SelectedIndex,1);
                eve.display();
            }
            else
            {
                MessageBox.Show("请先选中图片列表和左边列表！", "Error",
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button65_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex != -1 && listBox6.SelectedIndex != -1 && listBox8.SelectedIndex != -1)
            {
                eve.movesub(listBox5.SelectedIndex, listBox6.SelectedIndex,listBox8.SelectedIndex, 0);
                eve.displayfun(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox8.SelectedIndex);
                eve.display();
            }
            else
            {
                MessageBox.Show("请先选中图片列表和左边列表！", "Error",
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button66_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex != -1 && listBox6.SelectedIndex != -1 && listBox8.SelectedIndex != -1)
            {
                eve.movesub(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox8.SelectedIndex,1);
                eve.displayfun(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox8.SelectedIndex);
                eve.display();
            }
            else
            {
                MessageBox.Show("请先选中图片列表和左边列表！", "Error",
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button67_Click(object sender, EventArgs e)
        {
            if (listBox5.SelectedIndex != -1 && listBox6.SelectedIndex != -1 && listBox8.SelectedIndex != -1)
            {
                eve.movesubpos(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox8.SelectedIndex, Int16.Parse(textBox83.Text));
                eve.displayfun(listBox5.SelectedIndex, listBox6.SelectedIndex, listBox8.SelectedIndex);
                eve.display();
            }
            else
            {
                MessageBox.Show("请先选中图片列表和左边列表！", "Error",
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button68_Click(object sender, EventArgs e)
        {
            try
            {
                int newrow = Int32.Parse(textBox84.Text);
                int newcol = Int32.Parse(textBox85.Text);

                String[] ts = textBox86.Text.Split(new Char[] { ',' });
                if (ts.Length >= 6)
                {
                    int ox = Int32.Parse(ts[0]);
                    int oy = Int32.Parse(ts[1]);
                    int nx = Int32.Parse(ts[2]);
                    int ny = Int32.Parse(ts[3]);
                    int row = Int32.Parse(ts[4]);
                    int col = Int32.Parse(ts[5]);
                    if (map.adjust(newrow, newcol, ox, oy, nx, ny, row, col))
                    {
                        map.displayMap(-1,-1,0);
                        MessageBox.Show("调整完成！", "Error",
         MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show("出错，请检查参数值！", "Error",
         MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("出错，请检查参数值！", "Error",
          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch
            {
                MessageBox.Show("出错，请检查参数值！", "Error",
          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void panel11_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = map.getindex1(e.X,e.Y);
            if (p.X != -1)
            {
                label94.Text = "坐标:"+p.Y+","+p.X+"地图大小："+map.rows+"*"+map.cols;
            }
        }

        private void button69_Click(object sender, EventArgs e)
        {
            String[] ts = textBox87.Text.Split(new Char[] { ',' });
            if (ts.Length >= 4)
            {
                int ox = Int32.Parse(ts[0]);
                int oy = Int32.Parse(ts[1]);
                int nx = Int32.Parse(ts[2]);
                int ny = Int32.Parse(ts[3]);
                for (int i = ox; i <= nx; i++)
                    for (int j = oy; j <= ny; j++)
                    {
                        map.setImg(j, i, false);
                    }
                map.displayMap(-1,-1,0);
            }
            else
            {
                MessageBox.Show("出错，请检查参数值！", "Error",
      MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button70_Click(object sender, EventArgs e)
        {
            if (listBox6.SelectedIndex != -1 && listBox5.SelectedIndex != -1)
            {
                if (eve.toevent(listBox5.SelectedIndex, listBox6.SelectedIndex))
                {
                    listBox6_SelectedIndexChanged(sender, e);
                }
                else
                {
                    MessageBox.Show("出错，请检查！", "Error",
      MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }
        }






    }
}